package stubs;

import java.util.Iterator;
import java.util.LinkedList;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

import objetos.CategoriaObj;
import interfaces.Categoria;

public class CategoriaStub implements Categoria {

	@Override
	public boolean atualizarCadastro(CategoriaObj categoria) {
		try {
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "Categoria/atualizarCadastro");
			
			// Cria o arquivo xml
			OMElement payload = categoria.toOMElement();
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			
			return Boolean.parseBoolean(res.getText());
			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public LinkedList<CategoriaObj> buscarCategoriaPorNome(String Nome) {
		
		try {
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "Categoria/buscarCategoriaPorNome");
			
			// Cria arquivo xml
			OMFactory factory = OMAbstractFactory.getOMFactory();
			OMNamespace ns_element = factory.createOMNamespace("atributo", "atb");
			OMElement payload = factory.createOMElement("nome", ns_element);
			
			payload.setText(Nome);
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			
			Iterator<OMElement> it = res.getChildElements();
			LinkedList<CategoriaObj> list = new LinkedList<CategoriaObj>();
			
			while (it.hasNext()) {
				CategoriaObj categoria = new CategoriaObj();
				categoria.loadOMElement(it.next());
				list.add(categoria);
			}
			
			return list;
			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public boolean efetuarCadastro(CategoriaObj categoria) {
		try {
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "Categoria/efetuarCadastro");
			
			// Cria o arquivo xml
			OMElement payload = categoria.toOMElement();
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			
			return Boolean.parseBoolean(res.getText());
			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public boolean removerCadastro(int id) {
		try {
			
			// Cria Endpoint para envio do xml
			EndpointReference targetEPR = new EndpointReference(Bind.getServicesLocation() + "Categoria/removerCadastro");
			
			// Cria arquivo xml
			OMFactory factory = OMAbstractFactory.getOMFactory();
			OMNamespace ns_element = factory.createOMNamespace("atributo", "atb");
			OMElement payload = factory.createOMElement("id", ns_element);
			payload.setText(new Integer(id).toString());
			
			// Cria servico do cliente
			ServiceClient sc = new ServiceClient();
			
			// Cria opcoes do servico
			Options opt = new Options();
			opt.setTo(targetEPR);
			sc.setOptions(opt);
			
			// Envia e recebe resposta
			OMElement res;
			
			res = sc.sendReceive(payload);
			return Boolean.parseBoolean(res.getText());
			
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return false;
	}

}
